/*
 * JOY电子商务网运营事业部
 * 中国移动通信集团设计院有限公司
 * B2BJOY.COM  OPERATION  DIVISION
 * China Mobile Group Design Institute Co., Ltd
 */

package com.uhaou.system.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;

import org.apache.commons.lang.StringUtils;

import com.uhaou.common.base.*;
import com.uhaou.common.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.uhaou.system.model.*;
import com.uhaou.system.dao.*;
import com.uhaou.system.service.*;
import com.uhaou.system.vo.query.*;

/**
 * @author  jiangcheng
 * @version 1.0
 * @since 1.0
 */


@Service("sysRoleManager")
@Transactional
public class SysRoleManagerImpl extends BaseManager<SysRole,java.lang.String> implements ISysRoleManager{

	private ISysRoleDao sysRoleDao;
	private ISysFuncRoleDao sysFuncRoleDao;
	
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setSysRoleDao(ISysRoleDao dao) {
		this.sysRoleDao = dao;
	}
	public void setSysFuncRoleDao(ISysFuncRoleDao dao) {
		this.sysFuncRoleDao = dao;
	}
	public EntityDao getEntityDao() {
		return (EntityDao)this.sysRoleDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(SysRoleQuery query) {
		return sysRoleDao.findPage(query);
	}
	@Transactional(readOnly=true)
	public SysRole getById(java.lang.String id){
		return super.getById( id);
	}
	@Transactional
	public void removeById(java.lang.String id){
		super.removeById(id);
	}
	
	/** 插入数据 */
	@Transactional
	public void save(SysRole entity){
		super.save(entity);
	}

	/** 插入角色同时关联角色和权限即插入角色权限关联表数据  add by zhanghao*/
	@Transactional
	public void save(SysRole entity,String[] funcid ){
		this.sysRoleDao.save(entity);
		
		for(int i=0;funcid!=null&&i<funcid.length;i++){
			SysFuncRole tsf = new SysFuncRole();
			if(!funcid[i].equals("-1")&&!funcid[i].equals("")){
				tsf.setFuncId(funcid[i]);
				tsf.setRoleId(entity.getId());
				this.sysFuncRoleDao.save(tsf);
			}
		}
	}
	/**  更新角色同时，更新关联的角色和权限 即删除原来的关联数据，重新保存最新的关联  add by zhanghao */
	@Transactional
	public void update(SysRole entity,String[] funcid ){
		this.sysRoleDao.update(entity);
		
		//删除原来的关联数据
		SysFuncRoleQuery frquery = new SysFuncRoleQuery();
		Page frpage = null;
		List<SysFuncRole> frList = null;
		frquery.setPageSize(10000);
		frquery.setRoleId(entity.getId());
		frpage = sysFuncRoleDao.findPage(frquery);
		frList = frpage.getResult();
		for(int i=0;frList!=null&&i<frList.size();i++){
			SysFuncRole tfr = frList.get(i);
			sysFuncRoleDao.deleteById(tfr.getId());
		}
		
		//保存更新的关联数据
		for(int i=0;funcid!=null&&i<funcid.length;i++){
			SysFuncRole tsf = new SysFuncRole();
			if(!funcid[i].equals("-1")&&!funcid[i].equals("")){
				tsf.setFuncId(funcid[i]);
				tsf.setRoleId(entity.getId());
				this.sysFuncRoleDao.save(tsf);
			}
		}
	}
	
	
	/** 更新数据 */
	@Transactional
	public void update(SysRole entity){
		super.update(entity);
	}
	@Transactional(readOnly=true)
	public boolean isUnique(SysRole entity, String uniquePropertyNames){
		return super.isUnique( entity, uniquePropertyNames);
	}
	@Transactional(readOnly=true)
	public List<SysRole> findAll(){
		return super.findAll();
	}
	@Transactional
	public void saveOrUpdate(SysRole entity) {
		super.saveOrUpdate(entity);
	}
	/**
	 * 根据id的集合批量删除角色 zhanghao
	 * @param idsList List，list里面存放的是要删除的对象ID
	 * @return int
	 */
	public int deleteByIds(List idsList) {
		Map map = new HashMap();
		map.put("idList", idsList);
		return this.sysRoleDao.deleteByIds(map);
	}
}
